/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import helper.Helper;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.logging.Level;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;
import javax.faces.validator.ValidatorException;
import mensch.Player;
import org.apache.log4j.Logger;

/**
 *
 * @author rvlcek
 */
@ManagedBean(name="registerController")
@RequestScoped
public class Register {
    
    private final static Logger LOG = Logger.getLogger(RegisteredPlayers.class.getName());
    
    public Register(){
        LOG.debug("Konstruktor wird aufgerufen");
    }
    
    
    
    public boolean isSuccessfulRegistration() {
        return successfulRegistration;
    }

    public void setSuccessfulRegistration(boolean successfulRegistration) {
        this.successfulRegistration = successfulRegistration;
    }
    
    @ManagedProperty(value = "#{player}")
    private Player newPlayer;
    @ManagedProperty(value = "#{registeredPlayers}")
    private RegisteredPlayers rp;
    @ManagedProperty(value="false")
    boolean successfulRegistration;
    @ManagedProperty(value="true")
    boolean showPersonalData;
    
    public String register(){
        boolean regSuccess = getRp().addPlayer(newPlayer);
        if(regSuccess){
            successfulRegistration = true;
        }
        return "index";
    }
    public RegisteredPlayers getRp() {
        return rp;
    }

    public void setRp(RegisteredPlayers rp) {
        this.rp = rp;
    }

    public Player getNewPlayer() {
        return newPlayer;
    }

    public void setNewPlayer(Player p) {
        this.newPlayer = p;
    }
    
    public void validateBirthday(FacesContext ctx, UIComponent component, Object value) throws ValidatorException{
        String birthday = (String) value;
        DateFormat df = new SimpleDateFormat("dd.MM.yyyy");
        df.setLenient(false);
        try {
            df.parse(birthday);
        } catch (ParseException ex) {
            String errorMessage = Helper.getFacesMessage(ctx, "msg", "validateBirthday");
            FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, errorMessage, null);
            /*FacesMessage msg = Helper.getFacesMessage(ctx, FacesMessage.SEVERITY_ERROR,"validateBirthday");
            */throw new ValidatorException(msg); 
        }
    }
    
    public void showPersonalData(ValueChangeEvent v){
        Boolean display = (Boolean) v.getNewValue();
        if(display != null){
            showPersonalData = display;
        }
        FacesContext.getCurrentInstance().renderResponse();
    }

    public boolean isShowPersonalData() {
        return showPersonalData;
    }

    public void setShowPersonalData(boolean showPersonalData) {
        this.showPersonalData = showPersonalData;
    }
}
